app.controller('shopDetailInfoCtrl', ['$scope', '$window', 'shopDetailInfoService', 'toaster', 'utilService', '$modal','$state', function ($scope, $window, shopDetailInfoService, toaster, utilService, $modal,$state) {
    $scope.shopDetailInfoService = shopDetailInfoService;
    $scope.shop;

    //查询店铺信息
    $scope.findinfo = function () {
        shopDetailInfoService.findinfo($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shop = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }
    $scope.findinfo();

    //查询全部评价
    $scope.findAllEvaluate = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'user/evaluate',
        }).then(function (result) {
        });
    }

    $scope.findAllEvaluate();


    //查询店铺评价总分
    $scope.scorePercent = {};
    $scope.shopScore = 0.0;
    $scope.getShopEvaluateScore = function () {
        shopDetailInfoService.getShopEvaluateScore($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shopScore = result.data;
                    var percent = $scope.shopScore / 5 * 100;
                    $scope.scorePercent.left = percent + "%";
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.getShopEvaluateScore();

    //查询公告
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'notice',
        }).then(function (result) {
        });
    }


    //更改状态
    $scope.selectState = 0;
    $scope.changeState = function () {
        $scope.selectState = $scope.selectState == 0 ? 1 : 0;

        $scope.list = new Array();

        if ($scope.selectState == 0) {
            $scope.findAllEvaluate();
        } else if ($scope.selectState == 1) {
            $scope.query();
        }
    }

        //创建二维码
        $scope.createQrcode = function (shopId,qrcodeStatus) {
        if (qrcodeStatus==1) {
            openModal("之前的二维码将失效,确定重新生成二维码吗?");
            function openModal(msg) {
                utilService.modal($modal, $scope, {
                    title: "温馨提示",
                    controller: 'createQrcodeAgainCtrl',
                    message: msg,
                    data: shopId
                });
            }
        } else {
            shopDetailInfoService.createQrcode($scope, shopId).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        console.log("nihao");
                        $state.reload();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            })
        }
    }

    //打开图片
    $scope.openUrl = function (url) {
        $window.open(url);
    }

    //下载二维码图片
    $scope.downloadQrcode = function (shopId) {
        openModal("确定要下载二维码吗？");
        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "温馨提示",
                controller: "downloadQrcodeCtrl",
                message: msg,
                data:shopId
            });
        }
    }

    //二维码模态框显示
    $scope.imgMoShow=function(){
        $scope.shop.mo=true;
    }
    //二维码模态框隐藏
    $scope.imgMoHide=function(){
        $scope.shop.mo=false;
    }
}])
app.controller('createQrcodeAgainCtrl', ['$scope', 'shopDetailInfoService', 'modalData', '$modalInstance', 'toaster', function ($scope, shopDetailInfoService, modalData, $modalInstance, toaster) {
    $scope.modal = modalData;
    var shopId=modalData.data;
    $scope.isDelModal = true;

    $scope.ok = function () {
        if (!$scope.isDelModal) {
            return;
        }
        $scope.isDelModal = false;
        shopDetailInfoService.createQrcode($scope, shopId).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $scope.isDelModal = true;
                    $scope.findinfo();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

app.controller('downloadQrcodeCtrl', ['$scope', '$modalInstance', '$state', 'shopDetailInfoService', 'toaster', 'modalData',
    function ($scope,$modalInstance,$state,shopDetailInfoService,toaster,modalData) {
        $scope.modal = modalData;
        var shopId=modalData.data;
        $scope.isDelModal = true;

        $scope.ok = function () {
            if (!$scope.isDelModal) {
                return;
            }
            $scope.isDelModal = false;
            shopDetailInfoService.downloadQrcode($scope,shopId);
            $modalInstance.dismiss('success');
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        }
    }
]);